package com.blackline.auto.Report;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
//import com.newb.AutoDriver.AutoDriver;
import com.blackline.parameters.*;

/**
 * @author Joie
 * @version Aug, 2013
 * <p>The edit reports in batch test.
 */
public class EditStdReports {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(EditStdReports.class);
	UI UI=new UI();
	Parameters parameters;
	
    //Log in User as System Admin
	TestData testdata = new TestData("TestData/RunStdReportsWithAllRoles.xml");

	String t2Test2 = testdata.getInput("t2Test2");
	String user = testdata.getInput("user");
	String password = testdata.getInput("password");
	
    //Get standards reports which need run
	String[] StdReports =CommonMethods.getRunParameter("TestData/BL_StdReports.xml", "StdRpt");
	
	
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}
	
	
	@Test
	public void Edit_StdReports() throws Exception {
		
		//Login the test instance
		logger.info("You've selected site: " + t2Test2 + " with: " + user);
		UI.LogonPage.Login(driver, t2Test2, user, password);
		
		//Copy report in batch
		copyReports(StdReports);
		Thread.sleep(3000);
		driver.navigate().refresh();
		Thread.sleep(5000);
		//Editing report name in batch
		editValidRptName(StdReports);
		Thread.sleep(3000);
		//Editing report description in batch
		editRptDescription(StdReports);
		Thread.sleep(3000);
		//change report folder in batch
		changeRptFolder(StdReports);
	}
	
	/**
	 * Copy reports in batch
	 * @param rptName The report name
	 * Sample:<br>
	 * @throws Exception 
	 * 
	 */
	public void copyReports(String[] rptName) throws Exception {
		
		logger.info("=====Accounts Node=====");
		UI.Reporting.BlackLine_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(3000);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(3000);
		//Copy User Role Assignments report
		UI.Reporting.Name_text_field.clear();
		Thread.sleep(3000);
		UI.Reporting.Name_text_field.sendKeys(rptName[0]);
		Thread.sleep(3000);
        UI.Reporting.Refresh_button.click();
        Thread.sleep(3000);
        UI.Reporting.Copy_linkOfSearchedResult.click();
        Thread.sleep(3000);
        logger.info("Copy for " + rptName[0].toString());
		UI.Reporting.report_Name_forCopiedReport_textField.sendKeys("Copy for "+ rptName[0]);
		Thread.sleep(3000);
		UI.Reporting.Destination_Folder_forCopiedReport_dropDownList.click();
		Thread.sleep(3000);
		UI.Reporting.my_Favoriate_Reports_inDestinationFolder_dropDownListElement.click();
		Thread.sleep(3000);
		UI.Reporting.OK_button.click();
		Thread.sleep(3000);
		
		//Copy Analysis node reports
		logger.info("=====Analysis Node=====");
		driver.navigate().refresh();
		Thread.sleep(5000);
		UI.Reporting.BlackLine_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(3000);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(3000);
		UI.Reporting.Analysis_nodeInLeftTree.click();
		Thread.sleep(3000);
		for(int i=1;i<7;i++)
		{
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys(rptName[i]);
			Thread.sleep(3000);
            UI.Reporting.Refresh_button.click();
            Thread.sleep(3000);
            UI.Reporting.Copy_linkOfSearchedResult.click();
            Thread.sleep(3000);
            logger.info("Copy for " + rptName[i].toString());
			UI.Reporting.report_Name_forCopiedReport_textField.sendKeys("Copy for "+ rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Destination_Folder_forCopiedReport_dropDownList.click();
			Thread.sleep(3000);
			UI.Reporting.my_Favoriate_Reports_inDestinationFolder_dropDownListElement.click();
			Thread.sleep(3000);
			UI.Reporting.OK_button.click();
			Thread.sleep(3000);
		}
		
		//Copy Information node reports
		logger.info("=====Information Node=====");
		driver.navigate().refresh();
		Thread.sleep(5000);
		UI.Reporting.BlackLine_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(3000);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(3000);
		UI.Reporting.Information_nodeInLeftTree.click();
		Thread.sleep(3000);
		for(int i=7;i<11;i++)
		{
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys(rptName[i]);
			Thread.sleep(3000);
            UI.Reporting.Refresh_button.click();
            Thread.sleep(3000);
            UI.Reporting.Copy_linkOfSearchedResult.click();
            Thread.sleep(3000);
            logger.info("Copy for " + rptName[i].toString());
			UI.Reporting.report_Name_forCopiedReport_textField.sendKeys("Copy for "+ rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Destination_Folder_forCopiedReport_dropDownList.click();
			Thread.sleep(3000);
			UI.Reporting.my_Favoriate_Reports_inDestinationFolder_dropDownListElement.click();
			Thread.sleep(3000);
			UI.Reporting.OK_button.click();
			Thread.sleep(3000);
		}
		
		//Copy Status node reports
		logger.info("=====Status Node=====");
		driver.navigate().refresh();
		Thread.sleep(5000);
		UI.Reporting.BlackLine_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(3000);
		UI.Reporting.Accounts_nodeInLeftTree.click();
		Thread.sleep(3000);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(3000);
		UI.Reporting.Status_nodeInLeftTree.click();
		Thread.sleep(3000);
		for(int i=11;i<rptName.length;i++)
		{
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys(rptName[i]);
			Thread.sleep(3000);
            UI.Reporting.Refresh_button.click();
            Thread.sleep(3000);
            UI.Reporting.Copy_linkOfSearchedResult.click();
            Thread.sleep(3000);
            logger.info("Copy for " + rptName[i].toString());
			UI.Reporting.report_Name_forCopiedReport_textField.sendKeys("Copy for "+ rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Destination_Folder_forCopiedReport_dropDownList.click();
			Thread.sleep(3000);
			UI.Reporting.my_Favoriate_Reports_inDestinationFolder_dropDownListElement.click();
			Thread.sleep(3000);
			UI.Reporting.OK_button.click();
			Thread.sleep(3000);
		}
	
	}
	
	/**
	 * Edit valid report name in batch
	 * @param rptName The report name
	 * Sample:<br>
	 * @throws Exception 
	 * 
	 */
	public void editValidRptName(String[] rptName) throws Exception {
		
		UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		logger.info("Begin edit the reports with valid name");
		for(int i=0;i<rptName.length;i++)
		{
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys("Copy for " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			UI.Reporting.Edit_linkOfSearchedResult.click();
			Thread.sleep(3000);
			logger.info("Report:"+ rptName[i]+"Input valid value for name text field.");
			Thread.sleep(3000);
			UI.EditReport.reportName_textField.clear();
			Thread.sleep(3000);
			UI.EditReport.reportName_textField.clear();
			Thread.sleep(3000);
			UI.EditReport.reportName_textField.sendKeys("Update " + rptName[i]);
			Thread.sleep(3000);
			UI.EditReport.SaveReport_button.click();
			Thread.sleep(3000);
			driver.navigate().to("https://t2.blacklineondemand.com/Test2/Modules/Reports/Reporting.aspx");
			Thread.sleep(5000);
			UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys("Update " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			logger.info("[Pass] for editing report name");
		}
		
	}
	

	/**
	 * Edit report description in batch
	 * @param rptName The report name
	 * Sample:<br>
	 * @throws Exception 
	 * 
	 */
	public void editRptDescription(String[] rptName) throws Exception {
		
		UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		logger.info("Begin edit the reports description");
		for(int i=0;i<rptName.length;i++)
		{
			UI.Reporting.Description_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Description_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Description_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys("Update " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			UI.Reporting.Edit_linkOfSearchedResult.click();
			Thread.sleep(3000);
			logger.info("Report:"+ rptName[i]+" Update description.");
			Thread.sleep(3000);
			UI.EditReport.Description_textField.clear();
			Thread.sleep(3000);
			UI.EditReport.Description_textField.sendKeys("Update description " + rptName[i]);
			Thread.sleep(3000);
			UI.EditReport.SaveReport_button.click();
			Thread.sleep(3000);
			driver.navigate().to("https://t2.blacklineondemand.com/Test2/Modules/Reports/Reporting.aspx");
			Thread.sleep(5000);
			UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
			Thread.sleep(3000);
			UI.Reporting.Description_text_field.sendKeys("Update description " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			logger.info("[Pass] for editing report description");
		}
		
	}
	
	
	/**
	 * Change report folder in batch
	 * @param rptName The report name
	 * Sample:<br>
	 * @throws Exception 
	 * 
	 */
	public void changeRptFolder(String[] rptName) throws Exception {
		
		UI.Reporting.My_Favorite_Reports_nodeInLeftTree.click();
		Thread.sleep(3000);
		logger.info("Begin change the reports folder");
		for(int i=0;i<rptName.length;i++)
		{
			UI.Reporting.Description_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Description_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.clear();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys("Update " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			UI.Reporting.Edit_linkOfSearchedResult.click();
			Thread.sleep(3000);
			logger.info("Report:"+ rptName[i]+" change report folder.");
			Thread.sleep(3000);
			UI.EditReport.Folder_DropdownList.click();
			Thread.sleep(3000);
			UI.EditReport.UPS_Reports_inFolder_dropDownList.click();
			Thread.sleep(3000);
			UI.EditReport.SaveReport_button.click();
			Thread.sleep(3000);
			driver.navigate().to("https://t2.blacklineondemand.com/Test2/Modules/Reports/Reporting.aspx");
			Thread.sleep(5000);
			UI.Reporting.UPS_Reports_nodeInLeftTree.click();
			Thread.sleep(3000);
			UI.Reporting.Name_text_field.sendKeys("Update " + rptName[i]);
			Thread.sleep(3000);
			UI.Reporting.Refresh_button.click();
			Thread.sleep(3000);
			logger.info("[Pass] for change report folders.");
		}
		
	}
	

	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}